package com.kuang.aop;


//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.sy.pj.common.annotation.RequiredLog;
//import com.sy.pj.sys.pojo.SysLog;
//import com.sy.pj.sys.service.SysLogService;

import com.kuang.pojo.Logs;
import com.kuang.service.LogsService;
import com.kuang.util.Ip2Region;
import com.kuang.util.IpUtile;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;


@Aspect
@Component
public class AopLogs {
    //    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private LogsService logsService;
    public Long startTime = System.currentTimeMillis();


    @Pointcut("execution(* com.kuang.web.blog.*.*(..))")   // 切点表达式
//    @Pointcut("@annotation(com.kuang.web)")
//    @Pointcut("@annotation(com.kuang.web)")
    public void pointCut() {

    }

    @Around("pointCut()")
    public Object before(ProceedingJoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        long starttime = System.currentTimeMillis();
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
            System.out.println("proceed = " + proceed);
//            System.out.println("request.getRequestURL() = " + request.getRequestURL());
//            System.out.println("request.getMethod() = " + request.getMethod());
//            System.out.println("request.getRemoteAddr() = " + request.getRemoteAddr());
//            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        long endtime = System.currentTimeMillis();
        long time = endtime - starttime;
        saveLogs(joinPoint, time, proceed);


        return proceed;
    }

    /**
     * @param joinPoint 连接点，其实就是被增强功能的那个方法
     * @param time      方法执行时间
     * @param proceed   方法返回值
     */
    public void saveLogs(ProceedingJoinPoint joinPoint, Long time, Object proceed) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        StringBuffer requestURL = request.getRequestURL();
        String queryString = request.getQueryString();
        String name = joinPoint.getSignature().getName();
        String declaringName = joinPoint.getSignature().getDeclaringTypeName();
        StringBuilder stringBuilder = new StringBuilder(declaringName);  // 请求方法
        stringBuilder.append("/" + name);
        Logs logs = new Logs();
        String ip = IpUtile.getRemoteAddr(request); // ip 地址
        String ipRegion = null;
//        try {
//            ipRegion = Ip2Region.getIpRegion(ip);  // ip 的所在地
//        } catch (DbMakerConfigException e) {
//            e.printStackTrace();
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//        }
        logs.setIp(ip);
        logs.setAddress(ipRegion);
        logs.setMethod(stringBuilder.toString());
        logs.setParams(queryString);
        logs.setTime(time);
        logs.setReturnValue(proceed.toString());
        logs.setOperation(requestURL.toString());
        logs.setCreateTime(new Date());
        System.out.println("logs = " + logs);
        logsService.insertLogs(logs);
    }
}
